package com.c78.fz.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c78.fz.service.LoginService;
import com.c78.fz.common.result.Result;
import com.c78.fz.common.utils.JwtUtil;
import com.c78.fz.model.dto.LoginUser;
import com.c78.fz.model.entity.Accound;
import com.c78.fz.mapper.AccoundMapper;
import jakarta.annotation.Resource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【accound】的数据库操作Service实现
* @createDate 2025-01-19 21:21:27
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl extends ServiceImpl<AccoundMapper, Accound>
    implements LoginService {
    @Resource
    AuthenticationManager authenticationManager;
    @Resource
    AccoundMapper accoundMapper;

    @Override
    public Result login(String username, String password) {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username,password);
        try{
            Authentication authenticate = authenticationManager.authenticate(token);
            LoginUser loginUser = (LoginUser)authenticate.getPrincipal();
            Integer userId = loginUser.getUser().getId();
            QueryWrapper<Accound> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            Accound accound = accoundMapper.selectOne(queryWrapper);
            if(!ObjectUtils.isEmpty(accound)){
                accound.setUserInfo(JSON.toJSONString(loginUser));
                accoundMapper.updateById(accound);
            }else{
                accound = new Accound();
                accound.setUserId(userId);
                accound.setUserInfo(JSON.toJSONString(loginUser));
                accoundMapper.insert(accound);
            }
            String jwtToken = JwtUtil.createJWT(userId.toString());
            Map<String,Object> map = new HashMap<>();
            map.put("token",jwtToken);
            return Result.ok(map);
        }catch (Exception e){
            return Result.error("用户认证失败");
        }
    }

    @Override
    public Result logout() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userId = loginUser.getUser().getId();
        LambdaQueryWrapper<Accound> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Accound::getUserId,userId);
        accoundMapper.delete(queryWrapper);
        return Result.ok("注销成功");
    }
}




